//
// Created by wuzhe on 2022/4/18.
//

#include "FirstLoginState.h"
#include "OnlineState.h"
#include "InRoomState.h"

FirstLoginState::FirstLoginState(Player &player):PlayerState(player, PLAYER_STATUS::FIRST_LOGIN) {
//    player.updatePlayerInfo();

}

PlayerState *FirstLoginState::getCreateRole(std::string roleName) {
    player.setRoleName(roleName);

    player.space.getPlayerInfo(&player, [=](std::string res) {
        this->getCreateRoleCB(res);
    });

}

PlayerState *FirstLoginState::getCreateRoleCB(std::string res) {
    if (res != "nil") {
        auto i = res.find("3:");
        if (i != -1) {
            INFO(std::string("player ") + player.getRoleName() + " status changed: first login"
                                                                 "->in room");
            int roomId = std::stoi(res.substr(i + 2));
            player.onEnterRoom(roomId, true);
            return new InRoomState(player);
        }
    }
    player.sendAck();
    INFO(std::string("player ") + player.IP + " status changed: first login->online");
    return new OnlineState(player);
}
